package com.xingge.sdktest;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.provider.Settings;
import android.view.View;

import com.jolimark.UsbTool;
import com.xingge.sdktest.convert.PDFCallBack;
import com.xingge.sdktest.convert.PrintConverter;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        if (!hasPermissions(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
        }
    }


    public static boolean hasPermissions(Context context, String... perms) {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
            return true;
        }

        for (String perm : perms) {
            boolean hasPerm = (ContextCompat.checkSelfPermission(context, perm) ==
                    PackageManager.PERMISSION_GRANTED);
            if (!hasPerm) {
                return false;
            }
        }
        return true;
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            if (requestCode == 1) {

            }
        } else { //拒绝
            openSettingActivity();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    private void openSettingActivity() {
        Intent intent = new Intent();
        intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
        Uri uri = Uri.fromParts("package", getPackageName(), null);
        intent.setData(uri);
        startActivity(intent);
    }

    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.bt_usb:
                startActivity(new Intent(this, USBPrintActivity.class));
                break;

            case R.id.bt_wifi:
                startActivity(new Intent(this, WiFiPrintActivity.class));
                break;

            default:
                break;

        }
    }

    private void test(){
        String img = Environment.getExternalStorageDirectory().getAbsolutePath() + "/1.png";
        PrintConverter.PrintParams printParams = new PrintConverter.PrintParams();
        printParams.colorType = PrintConverter.ColorType.SINGLE;
        printParams.qualityType = PrintConverter.QualityType.NORMAL;
        printParams.sideType = PrintConverter.SideType.SINGLE;
        byte[] b = PrintConverter.convertImg(this,img, printParams);
        if(b != null){
            UsbTool.logd("b= " + b.length);
        } else {
            UsbTool.logd("b=null ");
        }

        String pdf = Environment.getExternalStorageDirectory().getAbsolutePath() + "/abcd.pdf";
        PrintConverter printConverter = new PrintConverter(this);
        printConverter.convertPDF(pdf, printParams, null, new PDFCallBack() {
            @Override
            public int onPageConverted(byte[] pageData, int[] index) {
                UsbTool.logd("onPageConverted= " + pageData.length + " : " + index.length);
                return pageData.length;
            }

            @Override
            public void onSuccess() {
                UsbTool.logd("onSuccess");
            }

            @Override
            public void onFail() {
                UsbTool.logd("onFail");
            }

            @Override
            public void onCancel() {
                UsbTool.logd("onCancel");
            }
        });

    } 

}
